/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package progappsproject1;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;

public class TeacherInterface extends javax.swing.JFrame {

    /**
     * Creates new form TeacherInterface
     */
    public TeacherInterface() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jPanel1 = new javax.swing.JPanel();
        mainPanel = new javax.swing.JPanel();
        refreshButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        studentList = new javax.swing.JTable();
        generateReportButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        jFormattedTextField1.setText("jFormattedTextField1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 325, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        mainPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        refreshButton.setIcon(new javax.swing.ImageIcon("C:\\Users\\PotPot\\Desktop\\refresh1.png")); // NOI18N
        refreshButton.setText("Refresh");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });
        mainPanel.add(refreshButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(750, 110, 80, 30));

        studentList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Student ID"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(studentList);
        studentList.getAccessibleContext().setAccessibleName("");

        mainPanel.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 10, 710, 320));

        generateReportButton.setIcon(new javax.swing.ImageIcon("C:\\Users\\PotPot\\Desktop\\genrep1.png")); // NOI18N
        generateReportButton.setText("Generate Report");
        generateReportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateReportButtonActionPerformed(evt);
            }
        });
        mainPanel.add(generateReportButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(750, 50, 130, 30));

        jButton1.setIcon(new javax.swing.ImageIcon("D:\\New folder\\2\\ProgAppsProject1\\nbproject\\scis_logo___saint_louis_university_by_thelivingzeus-d6c1b0s.png")); // NOI18N
        jButton1.setText("jButton1");
        jButton1.setPreferredSize(new java.awt.Dimension(150, 150));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        mainPanel.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(750, 190, 130, 120));

        jLabel2.setIcon(new javax.swing.ImageIcon("D:\\New folder\\2\\ProgAppsProject1\\nbproject\\wew4.jpg")); // NOI18N
        mainPanel.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, -90, 920, 570));

        getContentPane().add(mainPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, -1, 400));

        pack();
    }// </editor-fold>                        

    private void generateReportButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        GenerateReport gr = new GenerateReport();
        gr.setVisible(true);
        Interface1 it = new Interface1();
        gr.showReport(it.studId);
    }                                                    

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        updateTable();
        
    }                                             

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        Credits cred = new Credits();
        cred.setVisible(true);
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TeacherInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public  void run() {
                
                new TeacherInterface().setVisible(true);
                 }
        });
    }
    
    public void addDataToTable(int studId){
     try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/progapps";
            String name = "root";
            String password = "brizzalix";
            try {


           java.sql.Connection con = DriverManager.getConnection(url, name, password);
           java.sql.Statement stmts = null;
           String query = " select studid,concat(lastname,', ',firstname,'  ',middleinitial) as name,pcnumber,timein,"
                          + "timeout,status,numberofabsents,numberoflates from studentinfo";
           stmts = con.createStatement();
           
           if(getCurrentTime() <= 10){
               stmts.executeUpdate("update studentinfo set status='present' where studid =" +studId+"");
           }else if (getCurrentTime() > 10 && getCurrentTime() <= 15 ){
               stmts.executeUpdate("update studentinfo set status='late' where studid =" +studId+"");
               stmts.executeUpdate("update studentinfo set numberoflates=numberoflates+1 where studid =" +studId+"");
           }else{
               stmts.executeUpdate("update studentinfo set status='absent' where studid =" +studId+"");
               stmts.executeUpdate("update studentinfo set numberofabsents=numberofabsents+1 where studid =" +studId+"");
           }
                    
           
            stmts.executeUpdate("update studentinfo set timein=now() where studid =" +studId+"");
            updateTable();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
     

    }
    
    void updateTable(){
         try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/progapps";
            String name = "root";
            String password = "brizzalix";
            try {


           java.sql.Connection con = DriverManager.getConnection(url, name, password);
           java.sql.Statement stmts = null;
           String query = " select studid,concat(lastname,', ',firstname,'  ',middleinitial) as name,pcnumber,timein,"
                          + "timeout,status,numberofabsents,numberoflates from studentinfo";
          
           stmts = con.createStatement();          
           
           ResultSet rs = stmts.executeQuery(query);
           studentList.setModel(DbUtils.resultSetToTableModel(rs));
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    void studentLogOut(int studId){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/progapps";
            String name = "root";
            String password = "brizzalix";
            try {


           java.sql.Connection con = DriverManager.getConnection(url, name, password);
           java.sql.Statement stmts = null;

           stmts = con.createStatement();
           
           stmts.executeUpdate("update studentinfo set timeout=now() where studid =" +studId+"");          
           updateTable();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    
    int getCurrentTime(){
        Calendar cal = Calendar.getInstance();
    	cal.getTime();
    	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    	return ( Integer.parseInt(""+sdf.format(cal.getTime()).charAt(3) + sdf.format(cal.getTime()).charAt(4))  );
    }
    

        
    
    


    

    // Variables declaration - do not modify                     
    private javax.swing.JButton generateReportButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton refreshButton;
    private javax.swing.JTable studentList;
    // End of variables declaration                   


}
